require("../../../data/PCA.js");

var e = require("../../../utils/function.js"), t = require("../../../utils/util.js"), r = getApp(), a = require("../../../api.js");

Page({
    data: {
        img: e.driverImg,
        driverAge: "请选择",
        arrayDriverAge: [],
        getcarnum:'',
        driver: {
            city: [],
            handheld:"",
            workPhoto:"",
            workfPhptp:"",
            vehicle_fphoto:"",
            vehicle_photo:"",
            city_status: !1,
            cityTest: !1,
            name: null,
            name_status: !1,
            nameTest: !1,
            phone: null,
            phone_status: !1,
            phoneTest: !1,
            id: null,
            id_status: !1,
            idTest: !1,
            relative: null,
            relative_status: !1,
            relativeTest: !1,
            // bank: null,
            // bank_status: !1,
            bankTest: !1,
            workTime: -1,
            workTime_status: !1,
            workTimeTest: !1,
            idCard: null,
            idCard_status: !1,
            idCardTest: !1,
            driver: null,
            driver_status: !1,
            driverTime_start: "",
            driverTime_end: "",
            driverTime_status: !1,
            driverTimeTest: !1,
            idCardPhoto: "",
            idCardPhotoTest: "",
            driverPhoto: "",
            driverPhotoTest: "",
            carnum:'',
            nickname:"",
        },
        url_type: "",
        driverEffictTime: [ "6年", "10年", "长期" ],
        reverse: "",
        positive: "",
        idpositive: "",
        idreverse: "",
        region_open: !1,
        region: [],
        customItem: "全部",
        items: [
            {value: '1', name: '代驾'},
            {value: '2', name: '出租'},
        ],
        itemstype:""
    },
    edit: function(e) {
        r.Request({
            url: "driver/edit",
            method: "post",
            data: {
                work_number: e.detail.value
            }
        }).then(function(e) {
            console.log(e), 1 == e.code && r.showToast(e.msg);
        }), console.log(e.detail.value);
    },
   
    bindRegionChange: function(e) {
        console.log(e);
        var t = this.data.driver;
        r.city = e.detail.value, t.city = e.detail.value, wx.setStorageSync("driverRegisterCity", e.detail.value), 
        this.setData({
            driver: t,
            region_open: !0
        });
    },
    selectNameHandler: function() {
        var e = this.data.driver;
        e.name_status = !0, this.setData({
            driver: e
        });
    },
    selectedNameHandler: function(e) {
        var t = this.data.driver;
        t.name = e.detail.value, this.setData({
            driver: t
        });
    },
    selectedNicknameHandler: function(e) {
        var t = this.data.driver;
        t.nickname = e.detail.value, this.setData({
            driver: t
        });
    },
    selectPhoneHandler: function() {
        var e = this.data.driver;
        e.phone_status = !0, this.setData({
            driver: e
        });
    },
    selectedPhoneHandler: function(e) {
        var t = this.data.driver;
        t.phone = e.detail.value, this.setData({
            driver: t
        });
    },
    selectIdHandler: function() {
        var e = this.data.driver;
        e.id_status = !0, this.setData({
            driver: e
        });
    },
    selectedIdHandler: function(e) {
        var t = this.data.driver;
        t.id = e.detail.value, t.idTest = !0, this.setData({
            driver: t
        });
    },
    selectRelativeHandler: function() {
        var e = this.data.driver;
        e.relative_status = !0, this.setData({
            driver: e
        });
    },
    phoneTest: function(e) {
        return !("" == e || !/^1\d{10}$/.test(e)) || void wx.showToast({
            title: "请输入正确的电话号码格式",
            icon: "none",
            duration: 2e3
        });
    },
    selectedRelativeHandler: function(e) {
        var t = this.data.driver;
        t.relative = e.detail.value, this.setData({
            driver: t
        });
    },
    // selectBankHandler: function() {
    //     var e = this.data.driver;
    //     e.bank_status = !0, this.setData({
    //         driver: e
    //     });
    // },
    // selectedBankHandler: function(e) {
    //     var t = this.data.driver;
    //     t.bank = e.detail.value, this.setData({
    //         driver: t
    //     });
    // },
    bankTest: function(e) {
        for (var t = e.substr(e.length - 1, 1), r = e.substr(0, e.length - 1), a = new Array(), i = r.length - 1; i > -1; i--) a.push(r.substr(i, 1));
        for (var s = new Array(), d = new Array(), n = new Array(), o = 0; o < a.length; o++) (o + 1) % 2 == 1 ? 2 * parseInt(a[o]) < 9 ? s.push(2 * parseInt(a[o])) : d.push(2 * parseInt(a[o])) : n.push(a[o]);
        for (var v = new Array(), c = new Array(), l = 0; l < d.length; l++) v.push(parseInt(d[l]) % 10), 
        c.push(parseInt(d[l]) / 10);
        for (var u = 0, g = 0, h = 0, p = 0, m = 0, f = 0; f < s.length; f++) u += parseInt(s[f]);
        for (var w = 0; w < n.length; w++) g += parseInt(n[w]);
        for (var T = 0; T < v.length; T++) h += parseInt(v[T]), p += parseInt(c[T]);
        return m = parseInt(u) + parseInt(g) + parseInt(h) + parseInt(p), t == 10 - (parseInt(m) % 10 == 0 ? 10 : parseInt(m) % 10) || (wx.showToast({
            title: "请输入正确的银行卡号",
            icon: "none",
            duration: 2e3
        }), console.log("验证失败"), !1);
    },
    getDriverAge: function(e) {
        console.log(e);
        var t = this.data.driver;
        t.driverTime = parseInt(e.detail.value) + 1, console.log(t.driverTime), this.setData({
            driver: t
        });
    },
    getWorkAge: function() {
        for (var e = [], t = 1; t <= 45; t++) e.push(t + "年");
        this.setData({
            arrayDriverAge: e
        });
    },
    selectWorktimeHandler: function(e) {
        console.log("selectWorktimeHandler");
        var t = this.data.driver;
        t.workTime_status = !0, this.setData({
            driver: t,
            arrayDriverAge: arrayDriverAge,
            driverAge: e.detail.value
        });
    },
    selectedWorktimeHandler: function(e) {
        var t = this.data.driver;
        t.workTime = e.detail.value, t.workTimeTest = !0, this.setData({
            driver: t
        });
    },
    selectIdCardHandler: function() {
        var e = this.data.driver;
        e.idCard_status = !0, this.setData({
            driver: e
        });
    },
    selectedIdCardHandler: function(e) {
        var t = this.data.driver;
        t.idCard = e.detail.value, this.setData({
            driver: t
        });
    },
    IdCardTest: function(e) {
        if (!/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(e)) return wx.showToast({
            title: "身份证格式不正确！",
            icon: "none",
            duration: 2e3
        }), !1;
        if (18 == e.length) {
            for (var t = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2), r = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2), a = 0, i = 0; i < 17; i++) a += e.substring(i, i + 1) * t[i];
            var s = a % 11, d = e.substring(17);
            return 2 == s ? "X" == d || "x" == d || (wx.showToast({
                title: "身份证号码错误！",
                icon: "none",
                duration: 2e3
            }), !1) : d == r[s] || (wx.showToast({
                title: "身份证号码错误！",
                icon: "none",
                duration: 2e3
            }), !1);
        }
    },
    selectDriverAgeHandler: function(e) {
        console.log(e.detail.value);
        var t = this.data.driver;
        t.driverTime = e.detail.value, this.setData({
            driver: t
        });
    },
    selectDriverTimeHandler: function(e) {
        console.log(e);
    },
    selectedDriverTimeHandler: function(e) {
        var t = this.data, r = t.driver, a = t.driverEffictTime;
        console.log(e.detail.value), r.driverTime = a[e.detail.value], this.setData({
            driver: r
        });
    },
    startEfficeTime: function(e) {
        console.log(e);
        var t = this.data.driver;
        t.driverTime_start = e.detail.value, this.setData({
            driver: t
        });
    },
    endEfficeTime: function(e) {
        console.log(e);
        var t = this.data.driver;
        t.driverTime_end = e.detail.value, this.setData({
            driver: t
        });
    },
    intoPageHandler: function(e) {
        var t = this;
        wx.setStorageSync("driverss", t.data.driver), wx.navigateTo({
            url: e.currentTarget.dataset.skip
        });
    },
    // 提交
    personalInformationHandler: function() {
       if(this.data.itemstype==undefined){
            wx.showToast({
                title: "请完善司机信息",
                icon: "none"
            })
            return false;
       }
       console.log(this.data.itemstype)
        var e = this, t = (this.data.driver, {
            city: e.data.driver.city,
            name: e.data.driver.name,
            nickname: e.data.driver.nickname,
            phone: e.data.driver.phone,
            relative: e.data.driver.relative,
            // bank: e.data.driver.bank,
            workTime: e.data.driver.driverTime,
            idCard: e.data.driver.idCard,
            driverTime_end: e.data.driver.driverTime_end,
            driverTime_start: e.data.driver.driverTime_start,
            license_plate_num:wx.getStorageSync('drivercarnum'),
            idCardPhoto: wx.getStorageSync("idpositive"),
            idreverse: wx.getStorageSync("idreverse"),
            driverPhoto: wx.getStorageSync("positive"),
            reverse: wx.getStorageSync("reverse"),
            access_token: wx.getStorageSync("access_token"),
            handheld_id_card:wx.getStorageSync("handheld"),
            workPhoto:wx.getStorageSync("workPhoto"),
            workfPhptp:wx.getStorageSync("workfPhptp"),
            vehicle_photo:wx.getStorageSync("vehicle_photo"),
            vehicle_fphoto:wx.getStorageSync("vehicle_fphoto"),
            driver_identity:this.data.itemstype
            
        }), i = 0;
        console.log(t);
        console.log(e.data.driver.carnum);
        for (var s in t) "" != t[s] && null != t[s] || i++;
        return i > 0 ? (wx.showToast({
            title: "请完善司机信息",
            icon: "none"
        }), !1) : t.driverTime_start >= this.formatTime("Y-m-d", new Date()) ? (wx.showToast({
            title: "起始日期不能大于当前时间",
            icon: "none"
        }), !1) : t.driverTime_start > t.driverTime_end ? (wx.showToast({
            title: "有效截止时间不能小于生效时间",
            icon: "none"
        }), !1) : (wx.showLoading({}), void r.request({
            url: a.driver.information,
            method: "post",
            data: t,
            success: function(e) {
                wx.hideLoading(), 1 == e.code ? wx.showModal({
                    title: "提示",
                    content: e.msg,
                    showCancel: !1,
                    success: function() {
                        wx.canIUse("requestSubscribeMessage") && r.request({
                            url: a.message.getTmplIds,
                            method: "GET",
                            data: {
                                type: 3
                            },
                            success: function(e) {
                                wx.requestSubscribeMessage({
                                    tmplIds: e.data,
                                    complete: function(e) {
                                        console.log(e);
                                    }
                                });
                            }
                        }), setTimeout(function() {
                            wx.navigateBack({
                                delta: 2
                            });
                        }, 1000);
                    }
                }) : wx.showToast({
                    title: e.msg,
                    duration: 2e3,
                    icon: "none"
                });
            }
        }));
    },
    formatTime: function(e, t) {
        if (!e) return "";
        isNaN(Number(t)) ? isNaN(Number(t)) && (t = new Date(Date.parse(t))) : t = new Date(Number(t));
        for (var r = function(e) {
            return e < 10 ? "0" + e : e;
        }, a = [ "Y", "m", "d", "H", "i", "s" ], i = 0; i < a.length; i++) switch (a[i]) {
          case "Y":
            e = e.replace(a[i], t.getFullYear());
            break;

          case "m":
            e = e.replace(a[i], r(t.getMonth() + 1));
            break;

          case "d":
            e = e.replace(a[i], r(t.getDate()));
            break;

          case "H":
            e = e.replace(a[i], r(t.getHours()));
            break;

          case "i":
            e = e.replace(a[i], r(t.getMinutes()));
            break;

          case "s":
            e = e.replace(a[i], r(t.getSeconds()));
        }
        return e;
    },
    onLoad: function(e) {
        console.log(e);
        this.getAddress(), void 0 !== e.type ? (void 0 !== e.positive && this.setData({
            positive: wx.getStorageSync("positive")
        }), void 0 !== e.reverse && this.setData({
            reverse: wx.getStorageSync("reverse")
        }),
         void 0 !== e.reverse && this.setData({
            idreverse: wx.getStorageSync("idreverse")
        }), 
         void 0 !== e.reverse && this.setData({
            handheld: wx.getStorageSync("handheld")
        }), 
        void 0 !== e.positive && this.setData({
            idpositive: wx.getStorageSync("idpositive")
        })) : wx.removeStorageSync("driverss"), this.getWorkAge(), e.uid && r.shareCouponsMessage(e.uid);
    },
    carnum:function(e){
        console.log(e.detail);
        wx.setStorageSync('drivercarnum',e.detail)
        this.setData({
            carnum : e.detail
        })
    },
    getAddress: function() {
        var e = this;
        wx.getLocation({
            type: "gcj02",
            success: function(r) {
               
                t.resolutionAddress(r.latitude + "," + r.longitude, function(t) {
                    var r = t.data.result.address_component;
                    console.log(r);
                    e.setData({
                        region: [ r.province, r.city, r.district ]
                    });
                });
            }
        });
    },
    onReady: function() {},
    onShow: function() {
        var e = this, t = (wx.getStorageSync("driverss"), this.data.driver);
        r.request({
            url: a.driver.getinformation,
            method: "post",
            data: {},
            success: function(a) {
                console.log(a);
                e.setData({
                    getcarnum: a.data.license_plate_num
                })
                if (console.log(a), 1 == a.code) {
                    let vehicle_lice = JSON.parse(a.data.vehicle_licence)
                    let work_lice = JSON.parse(a.data.work_license)
                    // let handheld_id_card = JSON.parse(a.data.handheld_id_card)
                    // let handheld_id_card = 
                    console.log(vehicle_lice);
                    console.log(work_lice);

                    wx.setStorageSync("driving_license", a.data.driving_license), wx.setStorageSync("id_card_pic", a.data.id_card_pic), 
                    wx.setStorageSync("idpositive", a.data.id_card_pic.id_card_pic), wx.setStorageSync("idreverse", a.data.id_card_pic.id_card_fpic), 
                    wx.setStorageSync("positive", a.data.driving_license.driverPhoto), wx.setStorageSync("reverse", a.data.driving_license.driverfPhoto), 
                    wx.setStorageSync("handheld", a.data.handheld_id_card), 
                    wx.setStorageSync("workPhoto",work_lice.workPhoto), wx.setStorageSync("workfPhptp",  work_lice.workfPhptp), 
                    wx.setStorageSync("vehicle_photo",vehicle_lice.vehicle_photo), wx.setStorageSync("vehicle_fphoto", vehicle_lice.vehicle_fphoto), 
                    
                    // wx.setStorageSync("handheld", a.data.handheld)
                    t.city = a.data.city;
                    var i = 0 == !a.data.city.length;
                    t.name = a.data.real_name, t.phone = a.data.work_phone, t.id = a.data.id, t.relative = a.data.emergency, 
                    t.vehicle_photo=  vehicle_lice.vehicle_photo
                    t.vehicle_fphoto = vehicle_lice.vehicle_fphoto
                    t.handheld = a.data.handheld_id_card
                    t.workPhoto =  work_lice.workPhoto
                    t.workfPhptp =  work_lice.workfPhptp
                    t.carnum =  a.data.license_plate_num
                    // t.license_plate_num = 
                    // t.bank = a.data.bank, t.idCard = a.data.id_card, t.idCardPhotoTest = {
                    //     idpositive: a.data.id_card_pic.id_card_pic,
                    //     idreverse: a.data.id_card_pic.id_card_fpic
                    // },
                     t.driverPhotoTest = {
                        positive: a.data.driving_license.driverPhoto,
                        reverse: a.data.driving_license.driverfPhoto
                    }, t.driverTime = a.data.been_driving, t.driverTime_end = a.data.period[0], t.driverTime_start = a.data.period[1], 
                    e.setData({
                        driver: t,
                        region_open: i,
                        audit: a.data.audit
                    });
                    // 该111111111111111111111111111111
                } else t.city = r.city, t.idCardPhotoTest = {
                    idpositive: wx.getStorageSync("idpositive"),
                    idreverse: wx.getStorageSync("idreverse")
                }, t.handheld= wx.getStorageSync("handheld"),
                     t.workPhoto = wx.getStorageSync("workPhoto"),
                     t.workfPhptp = wx.getStorageSync("workfPhptp"),
                     t.vehicle_photo = wx.getStorageSync("vehicle_photo"),                 
                     t.vehicle_fphoto = wx.getStorageSync("vehicle_fphoto"),

                 t.driverPhotoTest = {
                    positive: wx.getStorageSync("positive"),
                    reverse: wx.getStorageSync("reverse")
                }, console.log(wx.getStorageSync("idpositive"), "----app.city"), console.log(r.city, "----app.city"), 
                e.setData({
                    driver: t
                });
                1 == a.code && 1 == a.data.audit ? e.setData({
                    isChange: !0
                }) : e.setData({
                    isChange: !1
                });
            }
        });
    },
    onShareAppMessage: function() {
        var e = r.shareAppMessage().title, t = r.shareAppMessage().imageUrl, a = r.shareAppMessage().desc, i = r.shareAppMessage().path;
        return console.log(r.shareAppMessage()), {
            title: e,
            imageUrl: t,
            desc: a,
            path: i
        };
    },
    radioChange(e) {
        console.log('radio发生change事件，携带value值为：', e.detail.value)
        this.setData({
            itemstype:e.detail.value
          })
        const items = this.data.items
        for (let i = 0, len = items.length; i < len; ++i) {
          items[i].checked = items[i].value === e.detail.value
        }
    
        this.setData({
          items
        })
      }
});